﻿/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2016/11/11
 * Time: 15:35
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;

namespace GeoFly
{
	/// <summary>
	/// Description of ToolForm.
	/// </summary>
	public partial class ToolForm : Form
	{
		public GridLayer grid = null;
		public GridLayer OutGrid = null;
		public ToolForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void ToolFormLoad(object sender, EventArgs e)
		{
			this.textBox_Extent.Text =
				grid.Extent.xmin + "," +
			grid.Extent.xmax + "," +
			grid.Extent.ymin + "," +
			grid.Extent.ymax;
			
	
		}
		public void SetParams(double west, double east, double south, double north)
		{
			
		}
		void Button2Click(object sender, EventArgs e)
		{
			FolderBrowserDialog dialog = new FolderBrowserDialog();
			if (dialog.ShowDialog() == DialogResult.OK) {
				this.textBox_Path.Text = dialog.SelectedPath;
			}
		}
		void Button1Click(object sender, EventArgs e)
		{
			string[] strs = this.textBox_Extent.Text.Split(new char[]{ ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
			
			double west = Convert.ToDouble(strs[0]);
			double east = Convert.ToDouble(strs[1]);
			double south = Convert.ToDouble(strs[2]);
			double north = Convert.ToDouble(strs[3]);
			double resol = Convert.ToDouble(this.textBox_Resol.Text);
			int rows = (int)((north - south) / resol);
			int cols = (int)((east - west) / resol);
			double[] lats = new double[rows];
			double[] lons = new double[cols];
			if (grid == null || grid.Values == null) {
				MessageBox.Show("请双击加载数据");
				return;
			}
			double[] latsSource = new double[grid.rowCount];
			double[] lonsSource = new double[grid.colCount];
			for (int row = 0; row < rows; row++) {
				lats[row] = south + row * resol;
			}
			for (int col = 0; col < cols; col++) {
				lons[col] = west + col * resol;
			}
			for (int row = 0; row < grid.rowCount; row++) {
				latsSource[row] = grid.DownLeft_Y + row * grid.resolution;
			}
			for (int col = 0; col < grid.colCount; col++) {
				lonsSource[col] = grid.DownLeft_X + col * grid.resolution;
			}
			
			double[,] result = null;
			GridLayer grid2=null;
			if(comboBox_ProjParam.Text=="Lambert")
			{
				result=Sampling.ReGridLambert(grid.Values, latsSource, lonsSource, 55,65,6000,1000,1000,35,107,30,60);
				MapProj.GaussKruger proj=new MapProj.GaussKruger();
				double[] values=proj.ToXY_Lambert(80,42,60,70,30,60);
			double lux=values[0];
			double luy=values[1]+6000*1999;
			grid2 = new GridLayer(400,500, -9999);
			grid2.DownLeft_X = lux;
			grid2.DownLeft_Y = luy;
			grid2.resolution = 6000;
			grid2.Values = result;
			this.OutGrid = grid2;	
			this.Close();
			return;
				
			} else if (comboBox1.Text == "Nearest") {
				result = Sampling.ReGridNearest(grid.Values, latsSource, lonsSource, lats, lons);
			} else
			
				result = Sampling.ReGridBilinear(grid.Values, latsSource, lonsSource, lats, lons);
			
			grid2 = new GridLayer(rows, cols, -9999);
			grid2.DownLeft_X = west;
			grid2.DownLeft_Y = south;
			grid2.resolution = resol;
			grid2.Values = result;
			this.OutGrid = grid2;	
			this.Close();
		}
		void Label2Click(object sender, EventArgs e)
		{
	
		}
		void ComboBox2SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.comboBox_ProjParam.Text != "Long-Lat") {
				this.textBox_ProjParam.Enabled = false;
			} else {
				this.textBox_ProjParam.Enabled = true;
			}
		}
		void TextBox_ProjParamTextChanged(object sender, EventArgs e)
		{
	
		}
	}
}
